//
//  LSZVideoDetailsVC.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/9/28.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZVideoDetailsVC.h"
#import "LCVideoHtmlDetailsController.h"

@interface LSZVideoDetailsVC ()

@end

@implementation LSZVideoDetailsVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.pageNumber = 1;
    // 设置导航控制器的代理为self
    self.navigationController.delegate = self;
}
#pragma mark - 查询有没有缓存的数据
-(void)QueryTheDataForCacheVideoDetails :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    NSString * strFile = [NSString stringWithFormat:@"%@_%@",jiangdao_shipin,self.strTitle];
    [LSZCacheHandling initWithfindPlist:strFile :^(BOOL isOk, NSDictionary * dic) {
        if (isOk == YES) {
            LSZVideoDetailsManager * MG  = [[LSZVideoDetailsManager alloc]initWithVideoDetailsManager:dic];
            [weakSelf.arrayMutData removeAllObjects];
            [weakSelf.arrayMutData addObjectsFromArray:MG.mutDataArray];            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [weakSelf.tableViewVideo reloadData];
            }];
        }else{
            [self initWithDataPage:page];
        }
    }];
}
#pragma mark - 请求的数据
-(void)initWithDataPage :(NSInteger)page
{
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic initWithVideoDetails:[NSString stringWithFormat:@"%ld",(long)page] parentId:weakSelf.strTitle :^(BOOL isOK, NSString * Str, LSZVideoDetailsManager * manager) {
        if(isOK == YES){
            if (page == 1) {
                [weakSelf.arrayMutData removeAllObjects];
                [weakSelf.arrayMutData addObjectsFromArray:manager.mutDataArray];
            }
            else{
                [weakSelf.arrayMutData addObjectsFromArray:manager.mutDataArray];
            }
            if(manager.mutDataArray.count == 0){
                [self.tableViewVideo.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.tableViewVideo reloadData];
        }else{
            [self setStringWarning:Str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrayMutData = [[NSMutableArray alloc]init];
    self.pageNumber = 1;
    [self QueryTheDataForCacheVideoDetails:self.pageNumber];
    [self setUITableview];
}
#pragma mark - ui
-(void)setUITableview
{
    [self.tableViewVideo registerClass:[LSZVideoDetailsHeaderCell class] forHeaderFooterViewReuseIdentifier:@"LSZVideoDetailsHeaderCell"];
    [self.tableViewVideo registerClass:[LSZVideoDetailsTableViewCell class] forCellReuseIdentifier:@"LSZVideoDetailsTableViewCell"];
    self.tableViewVideo.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 进入刷新状态后会自动调用这个block
            self.pageNumber = 1;
            [self QueryTheDataForCacheVideoDetails:self.pageNumber];
            [self.tableViewVideo.mj_header endRefreshing];
        });
    }];
    // 上拉刷新
    self.tableViewVideo.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 进入刷新状态后会自动调用这个block
            self.pageNumber ++;
            [self initWithDataPage:self.pageNumber];
            [self.tableViewVideo.mj_footer endRefreshing];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayMutData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 160;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LSZVideoDetailsHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZVideoDetailsHeaderCell"];
    [cell.imageviewBack   sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.labelTitle.text = [NSString stringWithFormat:@"%@",self.strTitle];
    [cell.butReturn addTarget:self action:@selector(clickReturn) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZVideoDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZVideoDetailsTableViewCell" forIndexPath:indexPath];
    LSZVideoDetailsMg * details = nil;
    if(self.arrayMutData.count >indexPath.row){
        details = self.arrayMutData[indexPath.row];
        cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
        [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:details.pic] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        cell.labelContent.text = [NSString stringWithFormat:@"%@",details.title];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZVideoDetailsMg * details = nil;
    details = self.arrayMutData[indexPath.row];
    //这个是宣传页的h5界面
//    LSZPromotionalGraphicDetailsVC *viewController = [[LSZPromotionalGraphicDetailsVC alloc] init];
//     viewController.url =  [NSString stringWithFormat:@"%@",details.playurl];
////    viewController.titleStr = details.title;
//     viewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
//    [self presentViewController:viewController animated:YES completion:nil];

    LCVideoDetailChangeHeaderController *viewController = [[LCVideoDetailChangeHeaderController alloc] init];
    LCVideoHomeListAboutModel *listAboutModel = [[LCVideoHomeListAboutModel alloc] init];
    listAboutModel.id =  details.id_id;
    listAboutModel.name =  details.title;
    listAboutModel.pic =  details.pic;
    listAboutModel.click =  details.click_num;
    listAboutModel.author =  self.strTitle;
    //listAboutModel.type =  details.click_num;
    viewController.listAboutModel = listAboutModel;
    [self.navigationController pushViewController:viewController animated:YES];

    NSArray * arrayGetData = [[NSArray alloc] initWithContentsOfFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo]];
    for (NSDictionary * dic in arrayGetData) {
        NSString * name = NSStr(dic[@"id_id"]);
        if ([name isEqualToString:details.id_id]) {
            return;
        }
    }
    //还没有添加过
    NSMutableArray * mutArrayAdd = [[NSMutableArray alloc]initWithArray:arrayGetData];
    NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:details.title,@"title",details.pic,@"imageViewUrl",self.strTitle,@"name",details.id_id,@"id_id",nil];
    [mutArrayAdd addObject:mutDic];
    [mutArrayAdd writeToFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo] atomically:YES];
}
#pragma mark - 导航返回键
-(void)clickReturn
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(UITableView *)tableViewVideo
{
    if (_tableViewVideo == nil) {
        _tableViewVideo = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        _tableViewVideo.delegate = self;
        _tableViewVideo.dataSource = self;
        //添加以下代码关闭估算行高
        _tableViewVideo.estimatedRowHeight = 0;
        _tableViewVideo.estimatedSectionHeaderHeight = 0;
        _tableViewVideo.estimatedSectionFooterHeight = 0;
        _tableViewVideo.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        _tableViewVideo.separatorColor = divider;
        [self.view addSubview:_tableViewVideo];
    }
    return _tableViewVideo;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
@end
